package m.store.action;

import com.alibaba.fastjson.JSONObject;
import m.base.action.BaseStatusAction;
import m.base.model.LevelInfo;
import m.base.model.UserInfo;
import m.base.service.StoreInfoService;
import m.base.util.LevelUtil;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.common.model.util.*;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.form.FormButton;
import m.html.form.FormEvent;
import m.html.form.FormInput;
import m.html.form.ModelForm;
import m.html.list.ListButton;
import m.html.list.ListCount;
import m.html.list.ListSortOrder;
import m.html.list.SuccessMethod;
import m.store.model.RepositoryInfo;
import m.store.service.RepositoryInfoService;
import m.system.cache.CacheUtil;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import manage.util.DictionaryUtil;

import java.sql.SQLException;
import java.util.List;

@ActionMeta(name = "store/repositoryInfo")
public class RepositoryInfoAction extends BaseStatusAction {
    private RepositoryInfo model;
    /**
     * 保存
     */
    @RequestMeta(transaction = true,logTitle = "基础数据",logDescription = "保存仓库信息")
    public JSONMessage doSave() throws Exception {
        PowerUtil.verifyPower(getStatusPower());
        return new JSONMessage(0,getService(RepositoryInfoService.class).save(model))
                .push("model", model);
    }

    @Override
    public void doRecoveryCallback() throws Exception {
        getService(RepositoryInfoService.class).verifyRepositoryNum();
        DictionaryUtil.clear(RepositoryInfo.class);
        CacheUtil.clear(RepositoryInfo.class);
    }

    @Override
    public void doDisableCallback() throws Exception {
        ModelQueryCheck.init(RepositoryInfo.class,model.getOid())
            .checkStrEq("defRepository",RepositoryInfo.No,"默认仓库不能停用")
            .doCheck(true,true);
        //getService(RepositoryInfoService.class).verifyRepositoryNum();
        DictionaryUtil.clear(RepositoryInfo.class);
        CacheUtil.clear(RepositoryInfo.class);
    }

    /*
     * 编辑仓库
     */
    public VueResult toEdit() throws Exception {
        PowerUtil.verifyPower(getStatusPower());//权限判断
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*","manageUser.*");
        }else{
            getService(RepositoryInfoService.class).verifyRepositoryNum();
            model=new RepositoryInfo();
            model.setDefRepository(ObjectUtil.isEmptyOid(PowerUtil.getDefRepository())?RepositoryInfo.Yes:RepositoryInfo.No);
            model.setStore(PowerUtil.getSessionStore(true));
        }
        ModelForm form = new ModelForm("仓库信息",3);
        form.addItem(
            FormInput.input("名称", "model.name").setSpan(3).setRequired(true).setPlaceholder("请输入名称"),
            FormInput.number("排序", "model.sort","#,##0",0).setSpan(3).setPlaceholder("请输入排序"),
            FormInput.selectOneAdmin("管理用户", "model","manageUser","model.store.oid","","{{model.manageUser.realname}}").setRequired(true).setSpan(3),
            FormInput.radio("默认","model.defRepository","m.store.model.RepositoryInfo!defRepository").setSpan(3)
        )
        .addData("model",model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    /*
     * 仓库列表
     */
    public VueResult manageList() throws Exception {
        VueListResult result=getVueListResult("仓库列表",RepositoryInfo.class,actionUrl("manageList"));
        result.setHeight(-1);
        result.addCol(toIndexColumn());
        result.addCol(toTextColumn("名称", "name", 200, true));
        result.addCol(toTextColumn("默认", "defRepository", 100, true).setDictOption(RepositoryInfo.class,"defRepository"));
        result.addCol(toTextColumn("管理用户", "manageUser.realname", 100, true));
        result.addCol(toIntegerColumn("排序", "sort", 100, true).setSort(ListSortOrder.ASC).setCount(ListCount.NONE));
        result.addCol(toStatusColumn(true,true));
        result.addCol(toSpaceColumn("操作", 100,
            ListButton.modal("修改", ButtonType.PRIMARY,actionUrl("toEdit"),450, SuccessMethod.REFRESH)
                .addParam("model.oid","oid")
        ).setFixed(Align.RIGHT));
        result.addButton(
            ListButton.modal("新增", ButtonType.PRIMARY,actionUrl("toEdit"),450, SuccessMethod.REFRESH)
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        result.addCondition(QueryCondition.eq("store.oid",PowerUtil.getSessionStoreOid()));
        return result;
    }
    @Override
    public String[] getStatusPower() {
        return new String[]{PowerUtil.Store};
    }

    @Override
    public RepositoryInfo getModel() {
        return model;
    }

    public void setModel(RepositoryInfo model) {
        this.model = model;
    }
}
